package indi.zhifa.engine.starter.redis;

import indi.zhifa.engine.redis.properties.RedisConfig;
import indi.zhifa.engine.redis.properties.SessionRedisProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.SpringSessionRedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;


@ConditionalOnProperty(prefix = "zhifa.redis.http-session",name = "enabled", havingValue = "true")
@Configuration
@EnableRedisHttpSession
public class RedisSessionAutoConfiguration {

    @ConfigurationProperties(prefix = "zhifa.redis.http-session")
    @Bean(name = "session-redis-properties")
    public SessionRedisProperties redisProperties(){
        return new SessionRedisProperties();
    }


    @SpringSessionRedisConnectionFactory
    @Bean(name = "session-redis-connection-factory")
    public RedisConnectionFactory redisConnectionFactory(@Qualifier(value = "session-redis-properties") SessionRedisProperties pRedisProperties){
        return RedisConfig.redisConnectionFactory(pRedisProperties);
    }

}
